<?php
/**
 * 账号管理类
 * 
 * @author  sunguide 
 * @version 1.0
 * @package main
 * @time 2013-9-24 11:39:08
 */
 
class AccountAction extends Action {
	private $_uid = '';
	public function _initialize(){ //初始化认证
		$this->_uid = session('uid');
		if(!$this->_uid) $this->display("Public:login");//(503,"对不起，您登录超时了，请重新登录！",1);
		//B('Authenticate', $action);
	}
    public function index(){
		
		$this->display();
    }
	//账号信息查看与修改
	public function accountInfo(){
		$Account = M('Account');
		$post = $this->_post();
		$uid = $this->_uid;
		if(trim($_POST['company_name']) == ''){
			
			$data = $Account->where("uid = $uid")->find();
		
			$result = array(
				'company_name' => $data['company_name'],
				'company_contacter' => $data['company_contacter'],
				'contact_telphone' => $data['contact_telphone'],
				'contact_mobilephone' => $data['contact_mobilephone'],
				'industry' => $data['industry'],
				'customer_manager' => $data['customer_manager'],
				'company_fax' => $data['company_fax'],
				'zip_code' => $data['zip_code'],
				'email' => $data['email'],
				'company_address' => $data['company_address'],
			);
			$this->assign('list',$result);
			
			$this->display();
		}else{
			$result = array(
				'uid' => $uid,
				'company_name' => $post['company_name'],
				'company_contacter' => $post['company_contacter'],
				'contact_telphone' => $post['contact_telphone'],
				'contact_mobilephone' => $post['contact_mobilephone'],
				'industry' => $post['industry'],
				'customer_manager' => $post['customer_manager'],
				'company_fax' => $post['company_fax'],
				'zip_code' => $post['zip_code'],
				'email' => $post['email'],
				'company_address' => $post['company_address'],
				'update_time' => date("Y-m-d H:i:s")
			);
			if($Account->save($result)){
				$this->ajaxReturn(1,"保存成功！",1);
			}else{
				$this->ajaxReturn(0,"保存失败！",0);
			}

			
		}	
	}
	//检查账号可用性
	public function checkAccount(){
		$GET = $this->_get();
		if($GET['account']){
			$Account = M('Account');
			
			$data['account'] = trim($GET['account']);
			$is = $Account->where($data)->find();
			if($is){
				$this->ajaxReturn(1,"该账号已经注册",1);
			}else{
				$this->ajaxReturn(0,"该账号可以注册",0);
			}
		}
	}
	//修改密码
	public function resetPassword(){
		$get = $this->_get();
		$uid = session('uid');
		if(!$uid) $this->redirect('Public:login');
		$Account = M('Account');
		if($get['password']==''){
			$this->error("请输入密码");
		}else if($get['password'] != $get['repassword']){
			$this->error("两次输入的密码不对!");			
		}else{
			
			$data = array(
					'uid'      => $uid,
					'password' => $get['password']
			);
			$Account->save($data);
		}
	}
	//公司签名修改
	public function signatureEdit(){
		$get = $this->_get();
		$Account = M('Account');
		if($get['signature']==''){
			$this->ajaxReturn(0,"请输入签名",0);
		}else{
			$uid = $this->_uid;
			if(str_len($get['signature']) > 20) $this->ajaxReturn(0,"签名不能超过20个字符",0);
			$data = array(
					'uid'      => $uid,
					'company_signature' => $get['signature']
			);
			$v = $Account->save($data);
			if($v){
				$this->ajaxReturn(1,"更新签名成功",1);
				
			}else{
				$this->ajaxReturn(0,"更新签名失败",0);
			}
					
		}
	
	}
	//账户充值显示
	public function charger(){
		
		$this->display();
	}
	//账户提交充值订单
	public function orderSubmit(){
		$post = $this->_post();
		$ChargeOrder = M('ChargeOrder');
		$amount = intval($post['amount']);
		$orderNumber = geneOrderNumber();
		$data = array(
			'owner_uid'     => $this->_uid,
			'order_number'  => $orderNumber,
			'order_amount'  => $amount,
			'user_remark'   => $post['remark'],
			'create_time'   => datetime(),
			'update_time'	=> datetime()
		);
		$v = $ChargeOrder->add($data);
		if($v){
			$this->ajaxReturn(1,"订单提交成功，请尽快汇款！",1);
		}else{
			$this->ajaxReturn(0,"订单提交失败，请稍后再试！",0);
		}
		$this->display();
	} 
	//账户充值记录
	public function chargerRecords(){
		$post = $this->_post();
		$ChargeOrder = M('ChargeOrder');
		import('ORG.Util.Page');// 导入分页类
		$uid = $this->_uid;
		$where = array(
			'owner_uid'     => $uid,
			'status'  => 0
		);
		$count      = $ChargeOrder->where($where)->count();// 查询满足要求的总记录数
		$Page       = new Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $ChargeOrder->where($where)->order('update_time')->limit($Page->firstRow.','.$Page->listRows)->select();
		//dump($where);
		if($list){
			$icount = count($list);
			for($i = 0; $i < $icount ;$i++){
				$list[$i]['handle_time']?1:$list[$i]['handle_time'] = "等待确认";
				$list[$i]['user_remark']?1:$list[$i]['user_remark'] = "无";	
			}
		}
		
		//dump($list);
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
	
	}
	//账户消费记录
	public function consumeRecords(){
		$post = $this->_post();
		$ConsumeOrder = M('ConsumeOrder');
		import('ORG.Util.Page');// 导入分页类
		$uid = $this->_uid;
		$where = array(
			'owner_uid'     => $uid,
		);
		$count      = $ConsumeOrder->where($where)->count();// 查询满足要求的总记录数
		$Page       = new Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $ConsumeOrder->where($where)->order('update_time')->limit($Page->firstRow.','.$Page->listRows)->select();
		//dump($where);
		//echo $ConsumeOrder->getLastSql();
		
		if($list){
			$icount = count($list);
			for($i = 0; $i < $icount ;$i++){
				$list[$i]['remark']?1:$list[$i]['user_remark'] = "无";	
			}
		}
		//dump($list);
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
	
	}
	
	//企业签名
	public function signature(){
		$Account = M('Account');
		$uid = $this->_uid;
		$data = $Account->where("uid = $uid")->find();
		
		$this->assign('signature',$data['company_signature']);
		$this->display();
	}
	//获取企业签名
	public function getSignature(){
		$Account = M('Account');
		$uid = $this->_uid;
		$data = $Account->field("uid,company_signature signature")->where("uid = $uid")->find();
		$error = $Account->getLastSql();
		if($data['signature']){
			$this->ajaxReturn($data,"获取签名成功",1);
		}else{
			$this->ajaxReturn(0,"获取签名失败或为空$error",0);
		}
	}
}